const {
	db,
	dbCmd,
	result_success,
	result_error,
	bluetoothCollection
} = require('../../common/constants')
module.exports = async function(params = {}) {
	if (!params.open_id) {
		return result_error('open_id为空')
	}
	let item = params.item
	if( params.device_id===item.deviceId){
		return result_error('绑定错误,请重试')
	}
	let bluetooth = await bluetoothCollection.where({
		device_id: params.device_id,
		open_id: params.open_id
	}).get()
	/* if (bluetooth.data.length > 0) {
		return result_error('设备已配对')
	} */
	bluetooth = await bluetoothCollection.where({
		open_id: params.open_id
	}).get()
	if (bluetooth.data.length === 2) {
		return result_error('只可以配对两只鞋垫')
	}
	if (bluetooth.data.length === 0) {
		await bluetoothCollection.add({
			open_id: params.open_id,
			device_id: params.device_id,
			name: params.name,
			characteristic_id: '0000FFF2-0000-1000-8000-00805F9B34FB',
			service_id: '0000FFF1-0000-1000-8000-00805F9B34FB',
			notify_characteristicId: '0000FFF3-0000-1000-8000-00805F9B34FB'
		})
		await bluetoothCollection.add({
			open_id: params.open_id,
			device_id: item.deviceId,
			name: params.name,
			characteristic_id: '0000FFF2-0000-1000-8000-00805F9B34FB',
			service_id: '0000FFF1-0000-1000-8000-00805F9B34FB',
			notify_characteristicId: '0000FFF3-0000-1000-8000-00805F9B34FB'
		})
	} else {
		await bluetoothCollection.add(params)
	}
	bluetooth = await bluetoothCollection.where({
		open_id: params.open_id
	}).get()
	if (bluetooth.data.length === 2) {
		return result_success(2)
	}
	return result_success()
}